package com.bdqn.tsyyds.controller;

import com.bdqn.tsyyds.entity.Permission;
import com.bdqn.tsyyds.entity.Student;
import com.bdqn.tsyyds.repository.StudentRepostiory;
import com.bdqn.tsyyds.service.Permission.PermissionService;
import com.bdqn.tsyyds.service.Student.StudentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class StudentController {
    @Resource
    StudentRepostiory studentRepostiory;
    @Resource
    StudentService studentService;
    @Resource
    PermissionService permissionService;

    @RequestMapping("/list")
    public String Lisr(Model model) {
        List<Student> students = studentRepostiory.findAll();
        model.addAttribute("list", students);
        return "user/list";
    }

    @RequestMapping(value = "/userListPage")
    public String userListPage(Model model,
                               String stdName,  //按照用户名查询
                               @RequestParam(required = false, defaultValue = "0") Long permissionId, //按照角色id查询
                               @RequestParam(required = false, defaultValue = "1") int pageIndex) { //当前显示的页码
        System.out.println("stdName：" + stdName);
        System.out.println("permissionId：" + permissionId);
        System.out.println("pageIndex：" + pageIndex);
        // Page<Student> userPage = studentRepostiory.getUserListPageService(usrName, roleId, pageIndex);

        Map param = new HashMap();
        param.put("stdName", stdName);
        param.put("permissionId", permissionId);
        //int pageIndex = 1;
        int size = 5; //每页显示数据量;
        //Sort sort = new Sort(Sort.Direction.ASC,"id");
        //控制分页的辅助类，可以设置页码(从 0 开始！！！)、每页的数据条数、排序等
        Pageable pageable = PageRequest.of(pageIndex - 1, size);
        Page<Student> userPage = studentService.findPageByMap(param, pageable);

        //启动的时候立即到mysql里面查询数据
        List<Student> list = studentRepostiory.findAll();
        model.addAttribute("list", list);

        //查询所有角色
        List<Student> roleList = studentRepostiory.findAll();
        model.addAttribute("role", roleList);

        model.addAttribute("userPager", userPage);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("stdName", stdName);
        model.addAttribute("permissionId", permissionId);
        return "user/list";
    }

    @RequestMapping("/add")
    public String okadd(Model model) {
        List<Permission> list = permissionService.finAll();
        model.addAttribute("list", list);
        return "user/add";
    }

    @RequestMapping("/save")
    public String oksave(Student student) {
        System.out.println(student.getStdName() + student.getPassword() + "性别" + student.getSex() + "年龄" + student.getAge() + "permission" + student.getPermission().getPermissionId());
        if (student.getSex().equals("1")) {
            student.setSex("男");
        } else if (student.getSex().equals("0")) {
            student.setSex("女");
        }
        System.out.println("性别" + student.getSex());
        Student count = studentRepostiory.save(student);
        studentRepostiory.flush();
        if (count != null && count.equals("")) {
            return "redirect:userListPage";
        } else {
            return "redirect:add";
        }
    }

    @RequestMapping("/update")
    public String okupadte(@RequestParam(value = "id") Long id, Model model) {
        Student students = studentRepostiory.findAllById(id);

        List<Permission> list = permissionService.finAll();
        model.addAttribute("list", list);

        model.addAttribute("students", students);
        return "user/update";
    }

    @RequestMapping("/del")
    public Object okdel(@RequestParam(value = "id") Long id) {
        Map map = new HashMap();
        System.out.println("进入了删除方法" + id);
        try {
            System.out.println("准备执行del");
            studentService.delel(id);
            System.out.println("执行了del");
            map.put("dek", "true");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("dek", "false");
        }
        return "redirect:userListPage";
    }
}
